package cn.ynrcb.personnelfile.sysadmin.dao;

import cn.ynrcb.personnelfile.admin.vo.OrgVo;
import cn.ynrcb.personnelfile.sysadmin.entity.SysOrgEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @author shiwenbo
 * @email 282398188@qq.com
 * @date 2019-06-19 16:31:01
 */
@Mapper
public interface SysOrgDao extends BaseMapper<SysOrgEntity> {

    /**
     * 查询所有机构列表
     */
    List<SysOrgEntity> querySysOrgPage(Page<SysOrgEntity> page, @Param("map") Map<String, Object> params);

    /**
     * 根据机构id查看子机构的数量
     */
    int childCount(List<Integer> asList);

    /**
     * 根据id更新组织的status
     */
    void updateStatusByIds(List<Integer> ids);

//    /**
//     * 新增微信部门
//     */
//    void saveWxDepartment(SysOrgEntity sysOrgEntity);

    /**
     * 去重查询pid列表
     */
    @Select("select DISTINCT p_id from sys_org where status=1 and p_id!=0")
    List<Integer> getAllOrgPids();

    /**
     * 查询组织用户树
     */
    @Select("SELECT o.id AS uid, 0 AS user_id, o.org_name AS NAME, o.p_id AS parent_id, FALSE AS isShowCheckBox, FALSE AS isUser  FROM sys_org o WHERE o.`status` = 1 UNION SELECT su.qywx_user_id AS uid, su.user_id, su.user_cn_name AS NAME, r.id AS parent_id, TRUE AS isShowCheckBox, TRUE AS isUser  FROM swj_org_user_rela AS r JOIN sys_user AS su ON r.user_id = su.user_id JOIN sys_org so ON ( r.id = so.id AND su.`status` = 1 AND so.`status` = 1 AND su.qywx_user_id <> '0' ) UNION SELECT su.qywx_user_id AS uid, su.user_id, su.user_cn_name AS NAME, su.id AS parent_id, TRUE AS isShowCheckBox, TRUE AS isUser  FROM sys_user su JOIN sys_org so ON (  su.id = so.id   AND su.`status` = 1  AND so.`status` = 1  AND su.qywx_user_id <> '0')")
    List<SysOrgEntity> orgAndUserTree();

    /**
     * 根据pid获取其下级部门的数量
     */
    @Select("select count(*) from sys_org where p_id = #{pId} AND `status` = 1")
    int getCountByPId(Long pid);

    /**
     * 根据pid查询下级机构和企业列表
     */
    @Select("select id,org_name,p_id,inner_code,org_type from sys_org where p_id = #{pid} and status = 1")
    List<SysOrgEntity> findByPid(Integer pid);

    /**
     * 根据id查询组织（包含机构和企业）
     */
    @Select("select * from sys_org where id = #{id} limit 1")
    SysOrgEntity findByOrgId(Long id);

    /**
     * 根据id查询组织是否存在
     */
    @Select("select count(*) from sys_org where id = #{id} limit 1")
    int countByOrgId(Long id);

    @Select("SELECT count(*) from sys_org as o LEFT JOIN swj_org_report_rela as r ON o.id = r.id where o.org_type = 2 and o.`status` = 1 and r.report_type like CONCAT('%',#{typeCode},'%') and o.town like CONCAT(#{town},'%')")
    Long getOrgNumByReportTypeAndTown(String typeCode, String town);

    /**
     * 根据relaMobile获取企业的id
     */
    @Select("SELECT distinct id FROM sys_org WHERE rela_mobile = #{mobile}")
    List<Integer> getOrgIdByMobile(String mobile);

    /**
     * 根据innerCode查询下级部门列表
     */
    @Select("select id,org_name,p_id,inner_code,org_type from sys_org where inner_code LIKE CONCAT('',#{innerCode},'%') and status = 1")
    List<SysOrgEntity> findByInnerCode(String innerCode);

    /**
     * 根据统一社会信用代码获取org
     */
    @Select("select * from sys_org where credit_code = #{creditCode} limit 1")
    SysOrgEntity getOrgByCreditCode(String creditCode);

    @Select("SELECT GROUP_CONCAT(id) as ids  from swj_org_user_rela where user_id = #{userId}")
    String findReportOrgByUserId(Integer userId);

    void saveOrg(SysOrgEntity org);

    /**
     * 根据org实体信息更新org
     *
     * @param org
     * @return
     */
    boolean updateOrg(SysOrgEntity org);

    @Select("SELECT * from sys_org where id = #{id} and rela_name = #{relaName} and rela_mobile = #{relaMobile}")
    SysOrgEntity findByRelaNameAndRelaMobile(Integer id, String relaName, String relaMobile);

    /**
     * 获取企业列表数据（用于远程搜索）
     *
     * @param params
     * @return
     */
    List<Map<String, String>> getOrgList(@Param("map") Map<String, Object> params);

    /**
     * 根据id获取orgName
     *
     * @param id
     * @return
     */
    @Select("SELECT org_name FROM sys_org WHERE `status` = 1 AND id = #{id}")
    String getOrgNameByOrgId(Integer id);

    /**
     * 获取所有组织的orgMap列表
     */
    List<SysOrgEntity> queryOrgTree();

    @Select("SELECT id,org_name FROM `sys_org` WHERE `status` = 1")
    List<OrgVo> findOrg();
}
